package com.ias.agdis_r.view.control;

import android.content.Context;
//import android.support.v4.view.ViewPager;
import androidx.viewpager.widget.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/** 
 * @author ZhiCheng Guo 
 * @version 2014年11月18日 下午12:44:59 精品界面多点触摸有bug,会导致pointerIndex out of range的异常, 
 *          所以加了这个view 
 */  
public class MutipleTouchViewPager extends ViewPager {

    private boolean mIsDisallowIntercept = false;
    private boolean noScroll = false;
    public MutipleTouchViewPager(Context context) {  
        super(context);  
    }  
  
    public MutipleTouchViewPager(Context context, AttributeSet attrs) {  
        super(context, attrs);  
    }
    public void setNoScroll(boolean noScroll) {
        this.noScroll = noScroll;
    }

    public boolean isNoScroll(){
        return noScroll;
    }
    @Override  
    public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {  
        // keep the info about if the innerViews do  
        // requestDisallowInterceptTouchEvent  
        mIsDisallowIntercept = disallowIntercept;  
        super.requestDisallowInterceptTouchEvent(disallowIntercept);  
    }  
  
    @Override  
    public boolean dispatchTouchEvent(MotionEvent ev) {  
        // the incorrect array size will only happen in the multi-touch  
        // scenario.  
        if (ev.getPointerCount() > 1 && mIsDisallowIntercept) {  
            requestDisallowInterceptTouchEvent(false);  
            boolean handled = super.dispatchTouchEvent(ev);  
            requestDisallowInterceptTouchEvent(true);  
            return handled;  
        } else {
            try {
                return super.dispatchTouchEvent(ev);
            }catch (Exception e){
                e.printStackTrace();
            }
            return true;
        }  
    }
    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        if (noScroll){
            return false;
        }
        return super.onTouchEvent(arg0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        if (noScroll){
            return false;
        }
        return super.onInterceptTouchEvent(arg0);
    }
}
